//
//  ChainNewsVC.m
//  Chain
//
//  Created by zhaosh on 2018/4/2.
//  Copyright © 2018年 zhaosh. All rights reserved.
//
#import "ChainNewsVC.h"
#import <VTMagic/VTMagic.h>
#import "ChainNewsListVC.h"
#import "ChainNavBarView.h"
#import "ChainNewsSearchVC.h"
#import "ChainNewsTypeModel.h"
#import "ChainCheckNewVersion.h"
#import "JHSysAlertUtil.h"

@interface ChainNewsVC ()<VTMagicViewDelegate,VTMagicViewDataSource>
@property (nonatomic,strong)VTMagicController *magicController;
@property (nonatomic,strong)NSMutableArray *titleListArr;
@property (nonatomic,strong)NSMutableArray *vcInfos;
@property (nonatomic,strong)NSMutableArray *dataArrayM;
@end

@implementation ChainNewsVC

-(NSMutableArray *)titleListArr{
    if (!_titleListArr) {
        _titleListArr = [NSMutableArray new];
        for (ChainNewsTypeModel *model in self.vcInfos) {
            NSString *title = model.title;
            [_titleListArr addObject:title];
        }
    }
    return _titleListArr;
}


-(NSMutableArray *)dataArrayM{
    if (!_dataArrayM) {
        _dataArrayM = [NSMutableArray new];
    }
    return _titleListArr;
}

-(NSMutableArray *)vcInfos{
    if (!_vcInfos) {
       _vcInfos = [NSMutableArray new];
    }
    return _vcInfos;
}

- (VTMagicController *)magicController
{
    if (!_magicController) {
        _magicController = [[VTMagicController alloc] init];
        _magicController.magicView.itemScale = 1.6;
        _magicController.magicView.navigationColor = [UIColor whiteColor];
        _magicController.magicView.separatorHeight = 1;
        _magicController.magicView.separatorColor = HEXCOLOR(@"#fb7575");
        _magicController.magicView.sliderHeight =9;
        _magicController.magicView.sliderWidth =9;
        _magicController.magicView.itemSpacing = 25;
        _magicController.magicView.sliderStyle = VTSliderStyleDefault;
        _magicController.magicView.bubbleRadius = 0;
        _magicController.magicView.layoutStyle = VTLayoutStyleDefault;
        _magicController.magicView.headerHidden = NO;
        _magicController.magicView.switchStyle = VTSwitchStyleDefault;
        _magicController.magicView.navigationHeight = 53.f;
        _magicController.magicView.needPreloading = NO;
        _magicController.magicView.dataSource = self;
        _magicController.magicView.delegate = self;
        
        _magicController.magicView.navigationInset=UIEdgeInsetsMake(0, 20, 0, 0);
        UIImageView *sliderView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 9, 9)];
        sliderView.image=[UIImage imageNamed:@"mine_group"];
        [_magicController.magicView setSliderView:sliderView];
    }
    return _magicController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setHeadView:self.view withLeftBtnText:@"" withHeadLable:@"辣头条" withRightBntText:@"nav_bar_search"];
    
    [self fetchNewsType];
    
    [ChainCheckNewVersion isHaveNewVersion:^(BOOL ishave,NSString * versionCode) {
        if (ishave) {
            [JHSysAlertUtil presentAlertViewWithTitle:@"提示" message:@"有新的更新了快去更新吧" cancelTitle:@"取消" defaultTitle:@"去更新" distinct:NO cancel:^{
                
            } confirm:^{
                //跳转到指定的url更新页
                NSURL *updateUrl = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/id"];
                [[UIApplication sharedApplication] openURL:updateUrl];
            }];
        }
    }];
}

-(void)fetchNewsType{
    NSDictionary *params = @{@"dictType":@"news_type"};
    NSString *requestUrl=[NSString stringWithFormat:@"%@%@",BASE_URL,@"/latoutiao/dict/getNewsTypeList"];
    [[ChainNetAPIClient sharedJsonClient]postRequestJsonDataWithPath:requestUrl withParams:params withHeader:nil andBlock:^(id data, NSError *error) {
        NSDictionary *info = [NSDictionary dictionaryWithDictionary:data];
        if (error) {
            NSString *message=[info objectForKey:@"message"];
            [MBProgressHUD  showMessage:message ToView:nil RemainTime:0.5];
        }else{
            NSString *success=[NSString stringWithFormat:@"%@",[info objectForKey:@"success"]];
            if ([success isEqualToString:@"0"]) {
                NSArray *tempArray=[data valueForKeyPath:@"value"];
                
                for (NSDictionary *dic in tempArray) {
                    ChainNewsTypeModel *item =[ChainNewsTypeModel yy_modelWithDictionary:dic];
                    [self.vcInfos addObject:item];
                }
                [self createNewsView];
            }
        }
    }];
}

-(void)createNewsView{
    //设置容器VC
    [self addChildViewController:self.magicController];
    [self.view addSubview:self.magicController.view];
    [self.magicController.magicView reloadData];
}
    
- (void)creativeNavBar{
    self.fd_prefersNavigationBarHidden = YES;
    UIImageView *bkV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    bkV.backgroundColor = HEXCOLOR(@"#ce0d0d");
    bkV.contentMode = UIViewContentModeScaleToFill;
    [self.view addSubview:bkV];
   
    __weak typeof(self) weakSelf=self;
    ChainNavBarView *navBar = [[ChainNavBarView alloc]initWithFrame:CGRectMake(0, -START_POSITION, ScreenWidth, kTopHeight) Type:ChainNavTypeTitleAndSearch placeholderStr:@"辣头条" vc:self.navigationController];
    self.navBarV = navBar;
    navBar.otherButtonBlock = ^{
        ChainNewsSearchVC *newsSearchVC=[[ChainNewsSearchVC alloc] init];
        [weakSelf.navigationController pushViewController:newsSearchVC animated:YES];
    };
    [self.magicController.view addSubview:navBar];
}

#pragma mark - VTMagicViewDataSource
- (NSArray<NSString *> *)menuTitlesForMagicView:(VTMagicView *)magicView {
    return self.titleListArr;
}

- (UIButton *)magicView:(VTMagicView *)magicView menuItemAtIndex:(NSUInteger)itemIndex {
    static NSString *itemIdentifier = @"itemIdentifier";
    UIButton *menuItem = [magicView dequeueReusableItemWithIdentifier:itemIdentifier];
    if (!menuItem) { 
        menuItem = [UIButton buttonWithType:UIButtonTypeCustom];
        [menuItem setTitleColor:HEXCOLOR(@"#999999") forState:UIControlStateNormal];
        [menuItem setTitleColor:HEXCOLOR(@"#333333") forState:UIControlStateSelected];
        menuItem.titleLabel.font = [UIFont systemFontOfSize:18.0f];
    }
    return menuItem;
}

- (UIViewController *)magicView:(VTMagicView *)magicView viewControllerAtPage:(NSUInteger)pageIndex
{
    ChainNewsTypeModel *model = self.vcInfos[pageIndex];
    NSString *title = model.title;
    ChainNewsListVC *viewController = [magicView dequeueReusablePageWithIdentifier:title];
    if (!viewController) {
        viewController = [[ChainNewsListVC alloc] init];
    }
    NSString *typeId = [NSString stringWithFormat:@"%@",model.code];
    viewController.newsTypeId = typeId;
    return viewController;
}

- (void)magicView:(VTMagicView *)magicView didSelectItemAtIndex:(NSUInteger)itemIndex{
    
}

-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)addAction{
    ChainNewsSearchVC *newsSearchVC=[[ChainNewsSearchVC alloc] init];
    [self.navigationController pushViewController:newsSearchVC animated:YES];
}
@end
